package flashcardtest;

import java.util.*; // for ArrayList
import java.io.*; 	// for BufferedReader and BufferedWriter
import java.util.Collections;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.FetchOptions;

public class FlashCardSet implements java.io.Serializable {
	private String title;	// name of the set of FlashCards. eg. "Spanish-English"
	private String category1;// eg. "Spanish"
	private String category2;// eg. "English"
	private Random random;	// for generating random integers



	/* Constructors */
	public FlashCardSet(String title, String category1, String category2) {
		this.title = title;
		this.category1 = category1;
		this.category2 = category2;
		this.random = new Random();
		
		
	}
	public FlashCardSet(String title) {
		// default category names
		this(title, "category1", "category2");
	}
	
	/* Public Interface */
	
	public String getTitle() 	{return this.title;}
	public String getCategory1()  {return this.category1;}
	public String getCategory2()  {return this.category2;}
	public boolean destroy() {  
		// deletes from the datastore Dictionary list the entry for this dictionary
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("Dictionary");
		q.addFilter("name", Query.FilterOperator.EQUAL, this.title);
		PreparedQuery pq = datastore.prepare(q);
		Entity doomed = pq.asSingleEntity();
		datastore.delete(doomed.getKey());
		return true;
	}
	public int size() {
		//DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		//Query q = new Query("Word");
		//q.addFilter("dictionary", Query.FilterOperator.EQUAL, this.title);
		//PreparedQuery pq = datastore.prepare(q);
		//return pq.countEntities(FetchOptions.Builder.withLimit(500));
		List<Entity> list = this.getList();
		return list.size();
	}
	
	public Iterator<Entity> iterator() {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("Word");
		q.addFilter("dictionary", Query.FilterOperator.EQUAL, this.title);
		PreparedQuery pq = datastore.prepare(q);
		return pq.asIterable().iterator();
	}
	public List<Entity> getList() {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("Word");
		q.addFilter("dictionary", Query.FilterOperator.EQUAL, this.title);
		PreparedQuery pq = datastore.prepare(q);
		return pq.asList(FetchOptions.Builder.withLimit(500));
	}
	public void sort(int sortByKey) {
		if (sortByKey == 1) {
		}
		else if (sortByKey == 2) {
		}
	}
	public String getaValueByKey1(String key){
		// returns a random entry of those that match 'key'
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("Word");
		q.addFilter("key1", Query.FilterOperator.EQUAL, key);
		PreparedQuery pq = datastore.prepare(q);
		return (String) pq.asSingleEntity().getProperty("key2");
	}
	public String getaValueByKey2(String key){
		// returns a random entry of those that match 'key'
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("Word");
		q.addFilter("key2", Query.FilterOperator.EQUAL, key);
		PreparedQuery pq = datastore.prepare(q);
		return (String) pq.asSingleEntity().getProperty("key1");
	}

	public List<Entity> lookupByKey1(String key) {
		/* returns a list of FlashCards for which word 1 matches key */
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("Word");
		q.addFilter("dictionary", Query.FilterOperator.EQUAL, this.title);
		q.addFilter("key1", Query.FilterOperator.EQUAL, key);
		PreparedQuery pq = datastore.prepare(q);
		return pq.asList(FetchOptions.Builder.withLimit(500));
	}
	public List<Entity> lookupByKey2(String key) {
		/* returns a list of FlashCards for which word 2 matches key */
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("Word");
		q.addFilter("dictionary", Query.FilterOperator.EQUAL, this.title);
		q.addFilter("key2", Query.FilterOperator.EQUAL, key);
		PreparedQuery pq = datastore.prepare(q);
		return pq.asList(FetchOptions.Builder.withLimit(500));
	}
	public boolean addCard(String key1, String key2){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Entity item = new Entity("Word");
		item.setProperty("dictionary", this.title);
		item.setProperty("key1", key1);
		item.setProperty("key2", key2);
		datastore.put(item);
		return true;
	}
	public boolean addCard(FlashCard newCard) {
		
		return true;
	}
	public boolean remove(String word1, String word2) {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		
		Query q = new Query("Word");
		q.addFilter("key1", Query.FilterOperator.EQUAL, word1);
		q.addFilter("key2", Query.FilterOperator.EQUAL, word2);
		PreparedQuery pq = datastore.prepare(q);
		
		Entity doomed = pq.asSingleEntity();
		
		datastore.delete(doomed.getKey());
	
		return true;
	}
	public boolean remove(FlashCard removeCard) {

		return true;
	}
	public FlashCard drawCard() {
		List<Entity> list = this.getList();
		int size = list.size();
		int index = 0;
		try {
			index = random.nextInt(size);
		}
		catch (IllegalArgumentException e) {
			size = 4;
			index = random.nextInt(size);
			
		}
		Entity e = list.get(index);
		String word1 = (String) e.getProperty("key1");
		String word2 = (String) e.getProperty("key2");
		FlashCard f = new FlashCard(word1, word2);
		return f;
	}
	
	
	
	
}